import { Context } from "egg";
import * as markdown from 'marked';
import ApiDecorator from "../decorator/apiDecorator";
import DtoDecorator from "../decorator/dtoDecorator";

export default class MarkdownUtil {

    /**生成react代码 */
    static buildReactApiCode(route: ApiDecorator.IRoute, ctx: Context) {
        let outs = route.out?.name ? DtoDecorator.params[route.out!.name] : undefined;
        let outKyes = outs && Object.keys(outs);
        let ins = route.in?.name ? DtoDecorator.params[route.in!.name] : undefined;
        let inKeys = ins && Object.keys(ins);
        let _code = "";
        if (inKeys && ins) {
            _code += `
# 实体  
            ${this.buildCode(`
export interface ${route.in?.name}{
    ${inKeys.map(p => {
                let item = ins![p];
                return `
    /**${item.desc} */
    ${item.name + ":" + item.type};
    `;
            }).join("")
                }
}
                `)
                }
                            `



        }
        if (outKyes && outs) {


        }


        _code += `
# hooks  
${this.buildCode(
            `
const result = useRequest(()=> ${ApiDecorator.replaceController(route.constructor.name)}.${route.funcName}() );

const result = usePage(()=> ${ApiDecorator.replaceController(route.constructor.name)}.${route.funcName}() );
`
        )}
        `;
        if (outKyes && outs) {
            _code += `
# 表格  
${this.buildCode(`
const table = useTable({
    rowKeys:"id",
    columns:[
        ${outKyes.map(p => {
                let items = outs![p];
                return `
                { title: "${items.desc}", dataIndex: "${items.name}", hideInSearch: true}`;
            }).join(",")}
    ]
});

<Table
    request={(params)=>{
        let {current,...searchKeys} =params;
        return ${ApiDecorator.replaceController(route.constructor.name)}.${route.funcName}({
            pageSize:current,
            ...searchKeys
        });
    }}
    {...table.props}
/>
`)}`
        }

        if (ins && inKeys) {
            _code += `
# 表单  
${this.buildCode(`
<ProForm onFinsh={async (values)=>{
  await ${ApiDecorator.replaceController(route.constructor.name)}.${route.funcName}(values);
}} >
    ${inKeys.map(p => {
                const items = ins![p];
                return `
            <ProFormText label="${items.desc}" name="${items.name}" />
        `;
            }).join("")}
</ProForm>

`)}

`


        }



        this.buildMarkdown(_code, ctx)
    }




    static buildCode(value: string) {
        return `
\`\`\`
${value}
\`\`\`
        `
    }

    static buildMarkdown(code: string, ctx: Context) {
        ctx.type = "html";
        ctx.body = `
<!doctype html>
<html>
<head>
  <meta charset="utf-8"/>
  <title>Marked in the browser</title>
  <link href="https://cdn.bootcdn.net/ajax/libs/github-markdown-css/5.1.0/github-markdown.min.css" rel="stylesheet">
</head>
<body>
  <div class="markdown-body" id="content">
      ${markdown.marked(code)}
  </div>
</body>
</html>
        `;
    }
};